package cn.ibizlab.todo.core.pms.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.math.BigInteger;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import cn.ibizlab.todo.util.errors.BadRequestAlertException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.context.annotation.Lazy;
import cn.ibizlab.todo.core.pms.domain.Task;
import cn.ibizlab.todo.core.pms.filter.TaskSearchContext;
import cn.ibizlab.todo.core.pms.service.ITaskService;

import cn.ibizlab.todo.util.helper.CachedBeanCopier;
import cn.ibizlab.todo.util.helper.DEFieldCacheMap;


import cn.ibizlab.todo.core.pms.client.TaskFeignClient;

/**
 * 实体[任务] 服务对象接口实现
 */
@Slf4j
@Service
public class TaskServiceImpl implements ITaskService {

    @Autowired
    TaskFeignClient taskFeignClient;


    @Override
    public boolean create(Task et) {
        Task rt = taskFeignClient.create(et);
        if(rt==null)
            return false;
        CachedBeanCopier.copy(rt,et);
        return true;
    }

    public void createBatch(List<Task> list){
        taskFeignClient.createBatch(list) ;
    }

    @Override
    public boolean update(Task et) {
        Task rt = taskFeignClient.update(et.getId(),et);
        if(rt==null)
            return false;
        CachedBeanCopier.copy(rt,et);
        return true;

    }

    public void updateBatch(List<Task> list){
        taskFeignClient.updateBatch(list) ;
    }

    @Override
    public boolean remove(Long id) {
        boolean result=taskFeignClient.remove(id) ;
        return result;
    }

    public void removeBatch(Collection<Long> idList){
        taskFeignClient.removeBatch(idList);
    }

    @Override
    public Task get(Long id) {
		Task et=taskFeignClient.get(id);
        if(et==null){
            et=new Task();
            et.setId(id);
        }
        else{
        }
        return  et;
    }

    @Override
    public Task getDraft(Task et) {
        et=taskFeignClient.getDraft();
        return et;
    }

    @Override
    public Task activate(Task et) {
         et=taskFeignClient.activate(et.getId(),et);
         return et;
    }
    @Override
    public Task assignTo(Task et) {
         et=taskFeignClient.assignTo(et.getId(),et);
         return et;
    }
    @Override
    public Task cancel(Task et) {
         et=taskFeignClient.cancel(et.getId(),et);
         return et;
    }
    @Override
    public boolean checkKey(Task et) {
        return taskFeignClient.checkKey(et);
    }
    @Override
    public Task close(Task et) {
         et=taskFeignClient.close(et.getId(),et);
         return et;
    }
    @Override
    public Task finish(Task et) {
         et=taskFeignClient.finish(et.getId(),et);
         return et;
    }
    @Override
    public Task otherUpdate(Task et) {
         et=taskFeignClient.otherUpdate(et.getId(),et);
         return et;
    }
    @Override
    public Task pause(Task et) {
         et=taskFeignClient.pause(et.getId(),et);
         return et;
    }
    @Override
    public Task restart(Task et) {
         et=taskFeignClient.restart(et.getId(),et);
         return et;
    }
    @Override
    @Transactional
    public boolean save(Task et) {
        if(et.getId()==null) et.setId((Long)et.getDefaultKey(true));
        if(!taskFeignClient.save(et))
            return false;
        return true;
    }

    @Override
    public void saveBatch(List<Task> list) {
        taskFeignClient.saveBatch(list) ;
    }

    @Override
    public Task start(Task et) {
         et=taskFeignClient.start(et.getId(),et);
         return et;
    }
    @Override
    public Task taskFavorites(Task et) {
         et=taskFeignClient.taskFavorites(et.getId(),et);
         return et;
    }
    @Override
    public Task taskNFavorites(Task et) {
         et=taskFeignClient.taskNFavorites(et.getId(),et);
         return et;
    }
    @Override
    @Transactional
    public Task updateDeadline(Task et) {
        //自定义代码
        return et;
    }

    @Override
    @Transactional
    public Task updateDesc(Task et) {
        //自定义代码
        return et;
    }

    @Override
    @Transactional
    public Task updateName(Task et) {
        //自定义代码
        return et;
    }

    @Override
    @Transactional
    public Task updatePri(Task et) {
        //自定义代码
        return et;
    }

    @Override
    @Transactional
    public Task updateType(Task et) {
        //自定义代码
        return et;
    }



	@Override
    public List<Task> selectByProject(Long id) {
        TaskSearchContext context=new TaskSearchContext();
        context.setSize(Integer.MAX_VALUE);
        context.setN_project_eq(id);
        return taskFeignClient.searchDefault(context).getContent();
    }

    @Override
    public void removeByProject(Long id) {
        Set<Long> delIds=new HashSet<Long>();
        for(Task before:selectByProject(id)){
            delIds.add(before.getId());
        }
        if(delIds.size()>0)
            this.removeBatch(delIds);
    }



    /**
     * 查询集合 分配给我的任务
     */
    @Override
    public Page<Task> searchAssignedToMyTask(TaskSearchContext context) {
        Page<Task> tasks=taskFeignClient.searchAssignedToMyTask(context);
        return tasks;
    }

    /**
     * 查询集合 数据集
     */
    @Override
    public Page<Task> searchDefault(TaskSearchContext context) {
        Page<Task> tasks=taskFeignClient.searchDefault(context);
        return tasks;
    }

    /**
     * 查询集合 我的收藏
     */
    @Override
    public Page<Task> searchMyFavorites(TaskSearchContext context) {
        Page<Task> tasks=taskFeignClient.searchMyFavorites(context);
        return tasks;
    }

    /**
     * 查询集合 项目任务
     */
    @Override
    public Page<Task> searchProjectTASK(TaskSearchContext context) {
        Page<Task> tasks=taskFeignClient.searchProjectTASK(context);
        return tasks;
    }

    /**
     * 查询集合 todo列表查询列表
     */
    @Override
    public Page<Task> searchTodoListTask(TaskSearchContext context) {
        Page<Task> tasks=taskFeignClient.searchTodoListTask(context);
        return tasks;
    }




}



